using System;
using System.Collections;
using System.DirectoryServices;

namespace BEGDC.CC.Entity
{
	#region State

	/// <summary>
	/// State object for NHibernate mapped table 'State'.
	/// </summary>
	[Serializable]
	public class State : System.IComparable
		{
		#region Member Variables
		
		protected int _id;
		protected string _stateCode;
		protected string _stateName;
		protected IList _stateFlowInstances;
		protected static String _sortExpression = "Id";
		protected static SortDirection _sortDirection = SortDirection.Ascending;

		#endregion

		#region Constructors

		public State() { }

		public State( string stateCode, string stateName )
		{
			this._stateCode = stateCode;
			this._stateName = stateName;
		}

		#endregion

		#region Public Properties

		public int Id
		{
			get {return _id;}
			set {_id = value;}
		}

		public string StateCode
		{
			get { return _stateCode; }
			set
			{
				if ( value != null && value.Length > 20)
					throw new ArgumentOutOfRangeException("Invalid value for StateCode", value, value.ToString());
				_stateCode = value;
			}
		}

		public string StateName
		{
			get { return _stateName; }
			set
			{
				if ( value != null && value.Length > 50)
					throw new ArgumentOutOfRangeException("Invalid value for StateName", value, value.ToString());
				_stateName = value;
			}
		}

		public IList StateFlowInstances
		{
			get
			{
				if (_stateFlowInstances==null)
				{
					_stateFlowInstances = new ArrayList();
				}
				return _stateFlowInstances;
			}
			set { _stateFlowInstances = value; }
		}

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
		#endregion
		
        #region IComparable Methods
        public int CompareTo(object obj)
        {
			if (!(obj is State))
				throw new InvalidCastException("This object is not of type State");
			
			int relativeValue;
			switch (SortExpression)
			{
				case "Id":
					relativeValue = this.Id.CompareTo(((State)obj).Id);
					break;
				case "StateCode":
						relativeValue = ( this.StateCode != null ) ? this.StateCode.CompareTo(((State)obj).StateCode) : -1;
					break;
				case "StateName":
						relativeValue = ( this.StateName != null ) ? this.StateName.CompareTo(((State)obj).StateName) : -1;
					break;
                default:
                    goto case "Id";
			}
            if (State.SortDirection == SortDirection.Ascending)
				relativeValue *= -1;
			return relativeValue;
		}
		#endregion
	}

	#endregion
}
